package com.atguigu.practice;

import java.util.Scanner;

public class ErgodicBinaryTree {
    TreeNode root;
    int index;

    public static void main(String[] args) {
        Scanner scaner = new Scanner(System.in);
        while(scaner.hasNext()){
            // 接收前序遍历的结果
            String str = scaner.nextLine();
            ErgodicBinaryTree tree = new ErgodicBinaryTree();
            tree.createBinaryTree(str, '#');
            tree.inOrder();
        }
    }

    void createBinaryTree(String preStr,char invalid){
        index=0;
        root=createBinaryTreeN(preStr,invalid);
    }
    TreeNode createBinaryTreeN(String preStr,char invalid){
        TreeNode treeRoot=null;
        if(index<preStr.length()&&preStr.charAt(index)!=invalid){
            //创建根节点
            treeRoot=new TreeNode(preStr.charAt(index));
            //创建根节点左子树
            index++;
            treeRoot.left=createBinaryTreeN(preStr,invalid);
            //创建根节点右子树
            index++;
            treeRoot.right=createBinaryTreeN(preStr,invalid);
        }
        return treeRoot;
    }
    public void inOrder(){
        inOrder(root);
        System.out.println();
    }
    public void inOrder(TreeNode root){
        if(root==null)return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val){
            this.val=val;
        }
    }
}
